package admin

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"zhuandan/data_source"
	"zhuandan/models"
)

func Userdsx(ctx *gin.Context) {
	var user []models.User
	data_source.Db.Where("dsx > 0").Find(&user)
	for _, v := range user {
		var pr []models.Procureorder
		data_source.Db.Where("fbuid = ?", v.ID).Find(&pr)
		var price float64
		cunzai := 1
		for _, p := range pr {
			if p.Status == 3 {
				cunzai = 2
			}
			if p.Status == 1 {
				price = price + p.Price*float64(p.Num)
			}

		}
		if v.Dsx != price {
			if cunzai == 1 {
				data_source.Db.Model(&models.User{}).Where("id = ?", v.ID).Update("dsx", price)
				cldsx := models.Chulidsx{
					Uid:       v.ID,
					Price:     v.Dsx,
					Yuanprice: price,
				}
				data_source.Db.Create(&cldsx)
			}

		}

	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Userddyywd(ctx *gin.Context) {
	request := ProcuresBody{}
	ctx.ShouldBind(&request)
	page := request.Page
	pagesize := request.Pagesize
	offsetNum := pagesize * (page - 1)
	var user []models.User
	data_source.Db.Where("ywdgzheopenid != ?", "").Where("ywddingyue = 1").Limit(pagesize).Offset(offsetNum).Find(&user)
	for _, v := range user {
		data := Userdingyue(v.Ywdgzheopenid)
		if data.Subscribe == 0 {
			data_source.Db.Model(&models.User{}).Where("id = ?", v.ID).Update("ywddingyue", 3)
		} else if data.Subscribe == 1 {
			data_source.Db.Model(&models.User{}).Where("id = ?", v.ID).Update("ywddingyue", 2)
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Userddyywddy(ctx *gin.Context) {
	request := ProcuresBody{}
	ctx.ShouldBind(&request)
	page := request.Page
	pagesize := request.Pagesize
	offsetNum := pagesize * (page - 1)
	var user []models.User
	data_source.Db.Where("ywddingyue = 3").Order("id desc").Limit(pagesize).Offset(offsetNum).Find(&user)
	for _, v := range user {
		data := Userdingyue(v.Ywdgzheopenid)
		if data.Subscribe == 1 {
			data_source.Db.Model(&models.User{}).Where("id = ?", v.ID).Update("ywddingyue", 2)
		} else {
			data_source.Db.Model(&models.User{}).Where("id = ?", v.ID).Update("ywddingyue", 1)
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Userddyywdif(ctx *gin.Context) {
	request := ProcuresBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var user models.User
	data_source.Db.Where("id = ?", id).First(&user)
	fmt.Println(user)
	data := Userdingyue(user.Ywdgzheopenid)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Userdingyue(openid string) (data T2) {
	//configwxtoken := Accesstokengzh()
	configwxtoken := "85_cL6Ev8W_5DqLYDrNX7bVlZbEdA4YzyVD71h1j8vRMDeXjUjxdwLAt3m2yslX1G2JAngewxeOrJsgq4wWcQlvAKJ9Nsk_V9_Nxmqgx6UhqRoRlg4Z9aSXNJ1r448HJJbAJAVXX"
	resp, err := http.Get("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + configwxtoken + "&openid=" + openid + "&lang=zh_CN")

	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	r := T2{}
	fmt.Println("dingyue----------", string(body))

	//把请求到的结构反序列化到专门接受返回值的对象上面
	err = json.Unmarshal(body, &r)
	if err != nil {
		fmt.Println(err)
	}
	return r

}

type T2 struct {
	Subscribe      int    `json:"subscribe"`
	Openid         string `json:"openid"`
	Language       string `json:"language"`
	SubscribeTime  int    `json:"subscribe_time"`
	Unionid        string `json:"unionid"`
	Remark         string `json:"remark"`
	Groupid        int    `json:"groupid"`
	TagidList      []int  `json:"tagid_list"`
	SubscribeScene string `json:"subscribe_scene"`
	QrScene        int    `json:"qr_scene"`
	QrSceneStr     string `json:"qr_scene_str"`
}
